/*
 * Copyright (C) 2010,2011.
 * AHCP Project
 * All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at 
 *
 *     http://www.apache.org/licenses/LICENSE-2.0 
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an "AS IS"
 * BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either 
 * express or implied. See the License for the specific language
 * governing permissions and limitations under the License.
 */
package org.jacp.demo.perspective;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import java.awt.event.ComponentListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.Map;

import javax.imageio.ImageIO;
import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JMenu;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.ScrollPaneConstants;
import javax.swing.UIManager;

import org.jacp.api.action.IActionListener;
import org.jacp.api.componentLayout.Layout;
import org.jacp.demo.ui.components.FlatSplitPaneDivider;
import org.jacp.swing.rcp.action.SwingAction;
import org.jacp.swing.rcp.componentLayout.SwingPerspectiveLayout;
import org.jacp.swing.rcp.perspective.ASwingPerspective;
import org.jdesktop.swingx.graphics.GraphicsUtilities;

import com.explodingpixels.macwidgets.IAppWidgetFactory;

/**
 * Main Perspective, creates basic UI Layout (three columns split vertical)
 * 
 * @author Andy Moncsek
 * 
 */
public class ImagePerspective extends ASwingPerspective {

	private JPanel mainPanel = null;
	private JScrollPane scrollPane = null;
	private JSplitPane panel;
	private JSplitPane panelMiddle;
	private boolean lock = false;
	private boolean init = true;
	private int currentLimit = 0;
	private int max = 200;
	private JPanel drawer;

	@Override
	public void handleBarEntries(final Map<Layout, Container> bars) {
		final Container toolBar = bars.get(Layout.NORTH);
		BufferedImage myPicture = null;
		try {
			myPicture = ImageIO.read(new File("images/Home.png"));
		} catch (final IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		final JButton add = new JButton(new ImageIcon(
				GraphicsUtilities.createThumbnail(myPicture, 15, 15)));
		add.putClientProperty("JButton.segmentPosition", "first");
		// add.putClientProperty("JButton.buttonType", "segmentedTextured");
		add.setBackground(new Color(255, 255, 255, 0));
		add.setOpaque(false);
		add.setFocusable(false);
		final IActionListener<ActionListener, ActionEvent, Object> listener = getActionListener();
		listener.getAction().addMessage("id01.id02",
				"message from ImagePerspective");
		listener.getAction().addMessage("id01.id03",
				"message from ImagePerspective");
		listener.getAction().addMessage("id01.id04",
				"message from ImagePerspective");
		add.addActionListener(listener.getListener());

		// /////////////////////////////////////////////////
		final Container bottomBar = bars.get(Layout.SOUTH);

		try {
			myPicture = ImageIO
					.read(new File("images/QuickLook or Preview.png"));
		} catch (final IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		final JButton resize = new JButton(new ImageIcon(
				GraphicsUtilities.createThumbnail(myPicture, 19, 19)));
		// resize.putClientProperty("JButton.buttonType", "segmentedTextured");
		resize.putClientProperty("JButton.segmentPosition", "first");
		resize.setFocusable(false);
		resize.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(final ActionEvent arg0) {
				lock = true;
				// final int LIMIT = panel.getDividerSize(); // full collapsed
				final int LIMIT = 0; // full collapsed
				final int MAX_LIMIT = max; // full extend
				// REVERSE true means divider is showing up!
				final Boolean reverse = (panel.getWidth() - panel
						.getDividerLocation()) == LIMIT ? true : false;
				currentLimit = reverse ? MAX_LIMIT : LIMIT;
				panel.setEnabled(false);
				if (reverse) {
					drawer.setVisible(true);
				}
				for (int i = 0; i < MAX_LIMIT; i++) {

					final int loc = (panel.getWidth() - panel
							.getDividerLocation());
					if (!reverse) {
						if (loc > LIMIT) {
							panel.setDividerLocation(panel.getDividerLocation() + 2);
						}
					} else {
						if (loc < MAX_LIMIT) {
							panel.setDividerLocation(panel.getDividerLocation() - 2);
						}
					}
					panel.repaint();

				}
				if (!reverse) {
					drawer.setVisible(false);
				}
				panel.setEnabled(true);
			}
		});

		bottomBar.add(resize);
		toolBar.add(add);

	}

	@Override
	public void handleMenuEntries(final JMenu arg0) {
		// TODO Auto-generated method stub

	}

	@Override
	public void handlePerspective(final SwingAction arg0,
			final SwingPerspectiveLayout layout) {
		if (mainPanel == null) {
			mainPanel = new JPanel();
			mainPanel.setLayout(new GridLayout(1, 1));
			// ################### Main layout ##################
			JPanel mainContent = new JPanel();

			// ################### first Splitpane ##################
			mainContent.setLayout(new GridLayout(1, 1));
			// Navigation Bar
			JPanel leftMenu = new JPanel(new GridLayout(1, 1));

			// Helper-Panel
			JPanel photoContent = new JPanel();

			JSplitPane splitpane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,
					leftMenu, photoContent);
			splitpane.setDividerLocation(200);
			splitpane.setDividerSize(2);
			splitpane.setOneTouchExpandable(true);
			mainContent.add(splitpane);

			// main Content, yes i mean pictures!
			photoContent.setLayout(new BorderLayout());
			// The Toolbar on the Bottom
			JPanel toolbar = new JPanel(new GridLayout(1, 1));
			photoContent.add(toolbar, BorderLayout.SOUTH);
			//
			JPanel pictures = new JPanel();
			photoContent.add(pictures, BorderLayout.CENTER);

			pictures.setLayout(new GridLayout(1, 1));
			JPanel picturebox = new JPanel();
			picturebox.setBackground(new Color(0x666666));
			drawer = new JPanel(new GridLayout(1, 1));
			panel = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, picturebox,
					drawer);
			panel.addComponentListener(new ComponentAdapter() {

				@Override
				public void componentResized(ComponentEvent e) {

					{
						panel.setDividerLocation(panel.getWidth()
								- currentLimit);
					}
				}

			});
			panel.setDividerSize(0);
			panel.setDividerLocation(876);
			pictures.add(panel);

			mainPanel.add(mainContent);

			// register ui leaf components and ui root component
			layout.registerTargetLayoutComponent("left", leftMenu);
			layout.registerTargetLayoutComponent("image", picturebox);
			layout.registerTargetLayoutComponent("detail", drawer);
			layout.registerTargetLayoutComponent("bottom", toolbar);
			layout.setRootComponent(mainPanel);
			final IActionListener<ActionListener, ActionEvent, Object> listener = getActionListener();
			listener.getAction().addMessage("id01.id02",
					new Dimension(720, 768));
			listener.getListener().actionPerformed(
					listener.getAction().getActionEvent());
		}

	}

	private JSplitPane getSplitPane(final int deviderLocation, final int max,
			final int split, final boolean move) {
		final JSplitPane pane = new JSplitPane(split);
		pane.setDoubleBuffered(true);
		pane.setDividerLocation(deviderLocation);
		this.max = max;
		pane.setBorder(BorderFactory.createLineBorder(
				UIManager.getColor("window"), 0));
		if (move) {
			// set locations
			pane.addComponentListener(new ComponentListener() {

				@Override
				public void componentShown(final ComponentEvent arg0) {
					pane.setDividerLocation(pane.getWidth() - deviderLocation);
				}

				@Override
				public void componentResized(final ComponentEvent arg0) {
					if (init) {
						pane.setDividerLocation(pane.getWidth()
								- deviderLocation);
						init = false;
					}
					if (!lock) {
						pane.setDividerLocation(pane.getWidth()
								- deviderLocation);
					}
				}

				@Override
				public void componentMoved(final ComponentEvent arg0) {
					// TODO Auto-generated method stub

				}

				@Override
				public void componentHidden(final ComponentEvent arg0) {
					// TODO Auto-generated method stub

				}
			});
		}
		return pane;
	}

	private JScrollPane getScrollPane(final int x, final int y,
			final Color borderColor, final int borderWidth,
			final boolean scrollable) {
		final JScrollPane panel = new JScrollPane();
		if (x != 0 && y != 0) {
			panel.setMaximumSize(new java.awt.Dimension(2147483647, 2147483647));
			panel.setMinimumSize(new java.awt.Dimension(10, 10));
			panel.setPreferredSize(new java.awt.Dimension(x, y));
		}
		if (!scrollable) {
			panel.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
			panel.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_NEVER);
		} else {
			IAppWidgetFactory.makeIAppScrollPane(panel);
		}
		if (borderColor != null) {
			panel.setBorder(BorderFactory.createLineBorder(borderColor, 0));
		}
		return panel;
	}

}
